﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System.Configuration;
using System.Data;


public partial class MastData_BranchList : BasePage
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Branch"] = "1=0";
        }
        if (Session["Branch"] != null)
        {
            this.dsBranch.FilterExpression = Session["Branch"].ToString();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn_Sch_Click(object sender, EventArgs e)
    {
        string name = this.txt_Name.Text.Trim().ToUpper();
        string where = "";
        if (name.Length > 0)
        {
            where = string.Format("Name LIKE '{0}%'", name);
        }

        this.dsBranch.FilterExpression = where;
        Session["Branch"] = where;
    }

    #region Branch
    protected void ASPxGridView1_Init(object sender, EventArgs e)
    {
        ASPxGridView grd = sender as ASPxGridView;
        if (grd != null)
        {
            grd.ForceDataRowType(typeof(Touchmax.XXBranch));
        }
    }
    protected void ASPxGridView1_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {

        e.NewValues["CustId"] = "0";
        e.NewValues["Address"] = " ";
        e.NewValues["Tel1"] = " ";
        e.NewValues["Tel2"] = " ";
        e.NewValues["Fax1"] = " ";
        e.NewValues["Fax2"] = " ";
        e.NewValues["Contact1"] = " ";
        e.NewValues["Contact2"] = " ";
        e.NewValues["Email1"] = " ";
        e.NewValues["Email2"] = " ";
        e.NewValues["Remark"] = " ";
        e.NewValues["NomInd"] = "N";
        e.NewValues["UeiNo"] = " ";

        string userId = HttpContext.Current.User.Identity.Name;
        e.NewValues["UserId"] = userId;
        e.NewValues["EntryDate"] = DateTime.Today;

    }
    protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {

        e.NewValues["CustId"] = GetNo("Branch").ToString();
        string userId = HttpContext.Current.User.Identity.Name;

        e.NewValues["UserId"] = userId;
        e.NewValues["EntryDate"] = DateTime.Today;
    }
    protected void ASPxGridView1_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            int custId = SafeValue.SafeInt(e.NewValues["CustId"], 0);
            if (custId > 0)
            {
                SetNewNo("Branch", custId);
            }
            Session["Branch"] = "CustId='" + custId + "'";
            this.dsBranch.FilterExpression = "CustId='" + custId + "'";
        }
    }
    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {

    }
    protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {

    }
    public int GetNo(string counter_id)
    {
        string sql_ivNo = "select COUNTER from XXSETUP where CATEGORY='" + counter_id + "'";
        int _InvN = SafeValue.SafeInt(Touchmax.Manager.ORManager.ExecuteScalar(sql_ivNo), 0) + 1;
        return _InvN;
    }
    public void SetNewNo(string counter_id, int oldNo)
    {
        string sql_updateInvoiceN = string.Format("update xxsetup set COUNTER='{0}' where category='{1}'", oldNo, counter_id);
        int flag =Touchmax.Manager.ORManager.ExecuteCommand(sql_updateInvoiceN);
    } 
    #endregion

    protected void btn_Export_Click(object sender, EventArgs e)
    {
        gridExport.WriteXlsToResponse("BranchList", true);
    }
}
